;;; GNU Guix --- Functional package management for GNU
;;; Copyright   2022 Stephen Paul Weber <singpolyma.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages riddim)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix build-system copy)
  #:use-module (guix build-system trivial)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages lua)
  #:use-module (guix hg-download))

(define-public squish
  (package
    (name "squish")
    (version "0.0.0-7d6070e5a096")
    (source
      (origin
        (method hg-fetch)
        (uri (hg-reference
          (url "https://code.matthewwild.co.uk/squish")
          (changeset "7d6070e5a096")))
        (sha256
          (base32
            "1gvcij7yssml5q77gp85hw7irslqml8dp8qnh4da6lnzik8w9jil"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f ; there are none
       #:phases
       (modify-phases %standard-phases
         (delete 'configure)
         (add-after 'unpack 'patch-generated-shebang
           (lambda* (#:key outputs #:allow-other-keys)
             (substitute* "Makefile"
               (("install squish") "install -D squish")
               (("/usr/local") (assoc-ref outputs "out")))
             (substitute* "squish.lua"
               (("#!/usr/bin/env lua") (string-append "#!" (which "lua")))))))))
    (native-inputs
     `(("lua" ,lua-5.2)))
    (synopsis
      "")
    (description
      "")
    (home-page "https://code.matthewwild.co.uk/squish")
    (license license:expat)))

(define prosody-source-0.10
  (origin
    (method hg-fetch)
      (uri (hg-reference
        (url "https://hg.prosody.im/0.10")
        (changeset "bb8486491b48")))
      (sha256
        (base32
          "188j0g3b2hia7adl7b13wn23j1al23aly51k08abmiqcd5s3dmwb"))))

(define-public lua-verse
  (package
    (name "lua-verse")
    (version "0.0.0-a9be85b2da17")
    (source
      (origin
        (method hg-fetch)
        (uri (hg-reference
          (url "https://code.matthewwild.co.uk/verse")
          (changeset "a9be85b2da17")))
        (sha256
          (base32
            "12r9i30fqkh367wrc5v9m71z6dnfsfrp5021xlyz5zdkjj8n2qy7"))))
    (build-system copy-build-system)
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-before 'install 'squish
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (outdir (string-append out "/share/lua/5.2"))
                    (outfile (string-append outdir "/verse.lua")))
             (invoke "squish"
               (string-append "--prosody=" (assoc-ref inputs "prosody-source")))
             (mkdir-p outdir)
             (call-with-output-file outfile
               (lambda (port)
                 (format port
                   "package.path = '~a/share/lua/5.2/?.lua;~a/share/lua/5.2/?.lua;~a/share/lua/5.2/?.lua;' .. package.path;~@
                    package.cpath = '~a/lib/lua/5.2/?.so;~a/lib/lua/5.2/?.so;~a/lib/lua/5.2/?.so;' .. package.cpath;\n"
                   (assoc-ref inputs "lua-socket")
                   (assoc-ref inputs "lua-expat")
                   (assoc-ref inputs "lua-sec")
                   (assoc-ref inputs "lua-socket")
                   (assoc-ref inputs "lua-expat")
                   (assoc-ref inputs "lua-sec"))
                 (call-with-input-file "verse.lua" (lambda (inport)
                   (dump-port inport port))))))))
         (delete 'install))))
    (inputs
     `(("lua-socket" ,lua5.2-socket)
       ("lua-expat" ,lua5.2-expat)
       ("lua-sec" ,lua5.2-sec)))
    (native-inputs
     `(("squish" ,squish)
       ("lua" ,lua-5.2)
       ("prosody-source" ,prosody-source-0.10)))
    (synopsis
      "")
    (description
      "")
    (home-page "https://code.matthewwild.co.uk/verse")
    (license license:expat)))

(define-public riddim
  (package
    (name "riddim")
    (version "1c2b8d10ceed")
    (source
      (origin
        (method hg-fetch)
        (uri (hg-reference
          (url "https://code.matthewwild.co.uk/riddim")
          (changeset version)))
        (sha256
          (base32
            "0yyajkfn4q7ak31xv7rfm0wxlp4xmck0l4jviblzymj7zgdqgvzx"))))
    (build-system copy-build-system)
    (arguments
     `(#:install-plan '(("riddim.lua" "bin/riddim"))
       #:phases
       (modify-phases %standard-phases
         (add-before 'install 'squish
           (lambda* (#:key inputs #:allow-other-keys)
             (let* ((verse (string-append (assoc-ref inputs "lua-verse") "/share/lua/5.2/verse.lua")))
               (substitute* "init.lua"
                 (("local verse =")
                   (string-append "package.preload['verse'] = (function(...)
                       return dofile(\"" verse "\");
                     end)\n\nlocal verse =")))
               (substitute* "squishy"
                 (("\"youtube\",") "\"youtube\", \"servercontact\"")))
             (invoke "squish" "--executable")
             (chmod "riddim.lua" #o755))))))
    (inputs
     `(("lua" ,lua-5.2)))
    (native-inputs
     `(("squish" ,squish)
       ("lua-verse" ,lua-verse)))
    (synopsis
      "")
    (description
      "")
    (home-page "https://code.matthewwild.co.uk/riddim")
    (license license:expat)))
